package com.weisw.polaris.admin.service.impl;

import com.weisw.polaris.admin.dao.UserMapper;
import com.weisw.polaris.admin.model.User;
import com.weisw.polaris.admin.service.UserService;
import com.weisw.polaris.common.util.DateTimeUtils;
import com.weisw.polaris.common.util.PoiUtils;
import com.weisw.polaris.core.exception.PolarisException;
import com.weisw.polaris.core.page.MybatisPageHelper;
import com.weisw.polaris.core.page.PageRequest;
import com.weisw.polaris.core.page.PageResult;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByName(String s){
        return userMapper.findByName(s);
    }

    @Override
    public int save(User record) {
        Integer id = null;
        if(record.getUserId() == null || record.getUserId() == 0) {
            // 新增用户
            userMapper.insert(record);
            id = record.getUserId();
        } else {
            // 更新用户信息
            userMapper.updateByPrimaryKey(record);
        }
        // 更新用户角色
        if(id != null && id == 0) {
            return 1;
        }
        return id;
    }

    @Override
    public int delete(User record) {
        return userMapper.deleteByPrimaryKey(record.getUserId());
    }

    @Override
    public int delete(List<User> records) {
        for(User record:records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) throws PolarisException{
        return MybatisPageHelper.findPage(pageRequest,userMapper);
    }


    @Override
    public File createUserExcelFile(PageRequest pageRequest) {
        PageResult pageResult = findPage(pageRequest);
        return createUserExcelFile(pageResult.getContent());
    }

    public static File createUserExcelFile(List<?> records) {
        if (records == null) {
            records = new ArrayList<>();
        }
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row0 = sheet.createRow(0);
        int columnIndex = 0;
        row0.createCell(columnIndex).setCellValue("No");
        row0.createCell(++columnIndex).setCellValue("ID");
        row0.createCell(++columnIndex).setCellValue("用户名");
//        row0.createCell(++columnIndex).setCellValue("昵称");
//        row0.createCell(++columnIndex).setCellValue("机构");
//        row0.createCell(++columnIndex).setCellValue("角色");
//        row0.createCell(++columnIndex).setCellValue("邮箱");
//        row0.createCell(++columnIndex).setCellValue("手机号");
//        row0.createCell(++columnIndex).setCellValue("状态");
//        row0.createCell(++columnIndex).setCellValue("头像");
//        row0.createCell(++columnIndex).setCellValue("创建人");
        row0.createCell(++columnIndex).setCellValue("创建时间");
//        row0.createCell(++columnIndex).setCellValue("最后更新人");
//        row0.createCell(++columnIndex).setCellValue("最后更新时间");
        for (int i = 0; i < records.size(); i++) {
            User user = (User) records.get(i);
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < columnIndex + 1; j++) {
                row.createCell(j);
            }
            columnIndex = 0;
            row.getCell(columnIndex).setCellValue(i + 1);
            row.getCell(++columnIndex).setCellValue(user.getUserId());
            row.getCell(++columnIndex).setCellValue(user.getUserName());
//            row.getCell(++columnIndex).setCellValue(user.getDeptName());
//            row.getCell(++columnIndex).setCellValue(user.getRoleNames());
//            row.getCell(++columnIndex).setCellValue(user.getEmail());
//            row.getCell(++columnIndex).setCellValue(user.getStatus());
//            row.getCell(++columnIndex).setCellValue(user.getAvatar());
//            row.getCell(++columnIndex).setCellValue(user.getCreateBy());
            row.getCell(++columnIndex).setCellValue(DateTimeUtils.getDateTime(user.getCreateTime()));
//            row.getCell(++columnIndex).setCellValue(user.getLastUpdateBy());
//            row.getCell(++columnIndex).setCellValue(DateTimeUtils.getDateTime(user.getLastUpdateTime()));
        }
        return PoiUtils.createExcelFile(workbook, "download_user");
    }
}
